﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;

namespace com.dz.ct.common
{
    public class NetSiteCatchManager
    {
        /// <summary>
        /// 页面超时时间
        /// </summary>
        const int c_TIME_OUT = 150000;

        /// <summary>
        /// 锁对象，防止线程并发
        /// </summary>
        static object m_ojb = new object();
        /// <summary>
        /// 读取页面内容
        /// </summary>
        /// <param name="i_url"></param>
        /// <param name="i_encoding"></param>
        /// <returns></returns>
        public static string ReadUrl(string i_url, Encoding i_encoding)
        {
            System.Threading.Thread.Sleep(200);
      

            Encoding t_encoding = null;
            string t_html = string.Empty;
            try
            {
                using (MemoryStream t_memory_stream = new MemoryStream())
                {
                    ReadPageCodeToStream(i_url, t_memory_stream, delegate(HttpWebResponse t_response)
                    {
                        t_encoding = i_encoding ?? GetEncoding(t_response);
                    });

                    t_memory_stream.Position = 0;//内存流复位以便于重新读取

                    using (StreamReader t_reader = new StreamReader(t_memory_stream, t_encoding))
                    {
                        t_html = t_reader.ReadToEnd();
                    }
                }
                if (!string.IsNullOrEmpty(t_html))
                {
                    t_html = t_html.Replace("\r", "").Replace("\n", "").Replace("\r\n", "");
                }
                return t_html;
            }
            catch (Exception ex)
            {

                LogHelper.Error("获取页面内容失败" + ex.ToString() + ",url=" + i_url);
                ex = null;
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取页面编码
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        private static Encoding GetEncoding(HttpWebResponse response)
        {

            Encoding t_encoding = Encoding.Default;
            if (response.CharacterSet != null)
            {
                try
                {
                    t_encoding = Encoding.GetEncoding(response.CharacterSet);
                }
                catch (ArgumentException ex)
                {
                    LogHelper.Error("获取页面编码失败" + ex.ToString());
                }
            }
            return t_encoding;
        }

        /// <summary>
        /// 读取页面内容到数据流
        /// </summary>
        /// <param name="i_url"></param>
        /// <param name="i_stream"></param>
        /// <param name="ResponseCallback"></param>
        private static void ReadPageCodeToStream(string i_url, Stream i_stream, Action<HttpWebResponse> ResponseCallback)
        {
            HttpWebRequest t_request = (HttpWebRequest)WebRequest.Create(i_url);
            t_request.Timeout = c_TIME_OUT;
            t_request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5 (.NET CLR";
            t_request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            t_request.Headers["Accept-Language"] = "en-us,en;q=0.5";
            t_request.Headers["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";

            using (HttpWebResponse t_response = (HttpWebResponse)t_request.GetResponse())
            {
                if (ResponseCallback != null)
                {
                    ResponseCallback(t_response);
                }

                Stream t_stream = t_response.GetResponseStream();

                byte[] t_buffer = new byte[10000];
                int t_count = 0;
                while ((t_count = t_stream.Read(t_buffer, 0, t_buffer.Length)) != 0)
                {
                    i_stream.Write(t_buffer, 0, t_count);
                }
            }
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="i_img_url"></param>
        public static int DownLoadImage(string i_img_url,long i_book_id)
        {
            lock (m_ojb)
            {
                WebClient t_web_client = new WebClient();
                string t_path = AppConfig.BookImgRoot;

                if (!Directory.Exists(t_path))
                {
                    Directory.CreateDirectory(t_path);
                }
                string t_file_name = Path.Combine(t_path, string.Format("{0}.jpg", i_book_id));
                try
                {
                    if (!File.Exists(t_file_name))
                    {
                        t_web_client.DownloadFile(i_img_url, t_file_name);
                       
                    }
                    return Consts.Success;
                
                }
                catch (Exception ex)
                {

                    LogHelper.Error("下载文件失败" + ex.ToString());
                    return Consts.Faild;
                }
            }
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="i_img_url"></param>
        /// <param name="i_file_name"></param>
        public static void DownLoadFile(string i_url, string i_file_name)
        {

            try
            {
                Uri u = new Uri(i_url);
                System.Net.HttpWebRequest mRequest = (HttpWebRequest)WebRequest.Create(u);
                mRequest.Method = "GET";
                mRequest.ContentType = "Application/octet-stream";
                HttpWebResponse wr = (HttpWebResponse)mRequest.GetResponse();


                Stream sIn = wr.GetResponseStream();
                FileStream fs = new FileStream(i_file_name, FileMode.Create, FileAccess.Write);
                long length = wr.ContentLength;
                int i = 0;
                long j = 0;

                byte[] buffer = new byte[1024];
                while ((i = sIn.Read(buffer, 0, buffer.Length)) > 0)
                {
                    j += i;
                    fs.Write(buffer, 0, i);


                }

                sIn.Close();
                wr.Close();

                fs.Close();

            }
            catch { }




        }

        /// <summary>
        /// 过滤字符
        /// </summary>
        /// <param name="i_text"></param>
        /// <returns></returns>
        public static string ReplaceContent(string i_text)
        {
            if (string.IsNullOrEmpty(i_text))
                return string.Empty;
            i_text = Regex.Replace(i_text, "'", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<!--.*?-->", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "http.*?com", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<a.*?>.*?</a>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "天天中文网", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<center>.*?</center>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = i_text.Replace("<a href=http://www.qidian.com>起点中文网 www.qidian.com 欢迎广大书友光临阅读，最新、最快、最火的连载作品尽在起点原创！</a>", "");
            i_text = Regex.Replace(i_text, "http.*?shouDa8.com", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "shouDa8.com", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "http.*?zcn.com", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "zcn.com", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = i_text.Replace("ahref=http://www.&gt;起点中文网www.欢迎广大书友光临阅读，最新、最快、最火的连载作品尽在起点原创！", "");
            i_text = Regex.Replace(i_text, "<[/]*?a.*?>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<script.*?>.*?</script>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<[/]*?iframe.*?>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<[/]*?table.*?>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<[/]*?tr[^<>]*>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<[/]*?td[^<>]*>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<font[^<>]*>.*?</font>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "</font>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<div.*?>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "</div>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<span[^<>]*>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "</span>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<[/]*?center[^<>]*>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<[/]*?dd[^<>]*>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<[/]*?dl[^<>]*>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<[/]*?tbody[^<>]*>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<[/]*?fieldset[^<>]*>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<[/]*?script>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<h[^<>]*>.*?</h.*?>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<h[^<>]*>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<[/]*?h[^<>]*>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<[/]*?p[^<>]{3,}>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<img.*?>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "【.*?】", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "\\[.*\\]", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "（.*?）", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "{.*?}", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<b[^<>]*>.*?</b>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<ul[^<>]*>.*?</ul>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<li[^<>]*>.*?</li>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "文字首发：", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "www.*?com", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "3Z中文网", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "四卷宝藏之谜", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = i_text.Replace("起点中文网  欢迎广大书友光临阅读，最新、最快、最火的连载作品尽在起点原创！", "");
            i_text = i_text.Replace("起点中文网欢迎广大书友光临阅读，最新、最快、最火的连载作品尽在起点原创！", "");
            i_text = i_text.Replace("起点中文网", "");
            i_text = i_text.Replace("尽在起点原创！", "");
            return i_text;
        }

        /// <summary>
        /// 确定是否是章节内容
        /// </summary>
        /// <param name="i_book_name"></param>
        /// <param name="i_chapter_name"></param>
        /// <param name="i_html"></param>
        /// <returns></returns>
        public static bool IsContainChapterName(string i_book_name, string i_chapter_name, string i_html)
        {

            //去掉空格

            i_book_name = Regex.Replace(i_book_name, "[【】（），！？(),!?;\\.；、……]", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);

            i_chapter_name = Regex.Replace(i_chapter_name, "[\\s【】（），！？(),!?;；\\.、……]", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);

            i_html = Regex.Replace(i_html, "[\\s【】（），！？(),!?;；\\.、……]", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            if (i_html.IndexOf(i_chapter_name) > -1 && i_html.IndexOf(i_book_name) > -1)
                return true;

            return false;
        }

        /// <summary>
        /// 判断是否是相同的章节
        /// </summary>
        /// <param name="i_chapter_source"></param>
        /// <param name="i_chapter_target"></param>
        /// <returns></returns>
        public static bool CompareChapterName(string i_chapter_source, string i_chapter_target)
        {
            if (i_chapter_source.Equals(i_chapter_target))
                return true;

            //去掉空格
            i_chapter_source = Regex.Replace(i_chapter_source, "\\s", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_chapter_source = Regex.Replace(i_chapter_source, "[【】（）a-zA-Z，！？(),!?;\\.；、……]", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_chapter_target = Regex.Replace(i_chapter_target, "\\s", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_chapter_target = Regex.Replace(i_chapter_target, "[【】（）a-zA-Z，！？(),!?;；\\.、……]", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);

            if (i_chapter_source.IndexOf(i_chapter_target) > -1 || i_chapter_target.IndexOf(i_chapter_source) > -1)
                return true;

            return false;
        }

        /// <summary>
        /// 是否是盗版网站
        /// </summary>
        /// <param name="i_url"></param>
        /// <returns></returns>
        public static bool IsPiraticSite(string i_url)
        {
            if (i_url.ToLower().IndexOf("tieba") >-1 || i_url.ToLower().IndexOf("bbs") > -1 || i_url.ToLower().IndexOf("vip") >-1)
                return false;

            if (i_url.ToLower().IndexOf("qidian.com") > -1)
                return false;

            if (i_url.ToLower().IndexOf("readnovel.com") > -1)
                return false;

            if (i_url.ToLower().IndexOf("hongxiu.com") > -1)
                return false;

            if (i_url.ToLower().IndexOf("xxsy.net") > -1)
                return false;

            if (i_url.ToLower().IndexOf("jjwxc.net") > -1)
                return false;

            if (i_url.ToLower().IndexOf("booksky.org") > -1)
                return false;

            if (i_url.ToLower().IndexOf("sina.com.cn") > -1)
                return false;

            if (i_url.ToLower().IndexOf("zhengheng.com") > -1)
                return false;


            if (i_url.ToLower().IndexOf("kuaikanshu.com") > -1)
                return false;

            if (i_url.ToLower().IndexOf("yunxuange.com") > -1)
                return false;

            if (i_url.ToLower().IndexOf("zhulang.com") > -1)
                return false;

            if (i_url.ToLower().IndexOf("rongshuxia.com") > -1)
                return false;

            if (i_url.ToLower().IndexOf("17k.com") > -1)
                return false;

            if (i_url.ToLower().IndexOf("lcread.com") > -1)
                return false;

            if (i_url.ToLower().IndexOf("book.qq.com") > -1)
                return false;

            return true;
        }

        /// <summary>
        /// 获取站点完整链接地址
        /// </summary>
        /// <param name="i_site_url"></param>
        /// <param name="i_part_url"></param>
        /// <returns></returns>
        public static string GetFullUrl(string i_site_url, string i_part_url)
        {
            if (i_site_url.IndexOf("#") > -1 || i_part_url.IndexOf("#") > -1)
                return string.Empty;

            if(i_part_url.IndexOf("http://")>-1)
                return i_part_url;

          

            if (!i_part_url.StartsWith("http") && !i_part_url.StartsWith("/"))
            {
                i_part_url = i_site_url.Substring(0, i_site_url.LastIndexOf("/") + 1) + i_part_url;
            }
           
            if (i_part_url.StartsWith("/"))
            {
                Uri t_url = new Uri(i_site_url);

                i_part_url ="http://"+ t_url.Host+ i_part_url;
            }
            return i_part_url;

        }

    }
}
